<!--
 * @Date: 2022-09-21 14:41:53
 * @LastEditors: LDH 574427343@qq.com
 * @LastEditTime: 2023-05-24 15:20:24
 * @FilePath: /flow-designer/src/components/nodeWrap.vue
-->
<template>
  <div class="flow-disigner-node-wrap">
    <div class="node-wrap" v-if="nodeConfig.nodeType !== 4">
      <div class="node-wrap-box"  :class="(nodeConfig.nodeType === 1 ? 'start-node ' : '') +(isTried && nodeConfig.error ? 'active error' : '')">
        <div class="banner"   :style="`background: rgb(${textColors[nodeConfig.nodeType]});`"></div>
        <div class="title" :style="`background: rgb(${bgColors[nodeConfig.nodeType]});color: rgb(${textColors[nodeConfig.nodeType]});`">
          <span :style="`color: rgb(${textColors[nodeConfig.nodeType]});`" v-if="nodeConfig.nodeType === 1">{{ nodeConfig.nodeName }}</span>
          <template v-else>
            <input
                v-if="isInput"
                type="text"
                class="flow-input editable-title-input"
                @blur="blurEvent()"
                @focus="$event.currentTarget.select()"
                v-focus
                v-model="nodeConfig.nodeName"
                :placeholder="defaultText"
            />
            <span v-else class="editable-title" @click="clickEvent(index)" :style="`color: rgb(${textColors[nodeConfig.nodeType]});`">{{ nodeConfig.nodeName }}</span>
            <i class="flowicon flowicon-close close" @click="delNode" v-if="nodeConfig.nodeType !== 6"></i>
          </template>
        </div>
        <div class="content" @click="setPerson">
          <div class="text">
            <span class="placeholder" v-if="!showText">请选择{{defaultText}}</span>
            {{showText}}
          </div>
          <i class="flowicon flowicon-right arrow"></i>
        </div>
        <div class="error_tip" v-if="isTried && nodeConfig.error">
          <i class="flowicon flowicon-exclamation-circle"></i>
        </div>
      </div>
      <addNode v-model:childNodeP="nodeConfig.childNode" />
    </div>
    <div class="branch-wrap" v-if="nodeConfig.nodeType === 4">
      <div class="branch-box-wrap">
        <div class="branch-box">
          <button class="add-branch" @click="addTerm">添加条件</button>
          <div class="col-box" v-for="(item, index) in nodeConfig.conditionExpressionList" :key="index">
            <div class="condition-node">
              <div class="condition-node-box">
                <div class="auto-judge" :class="isTried && item.error ? 'error active' : ''">
                  <div class="sort-left" v-if="index != 0" @click="arrTransfer(index, -1)">&lt;</div>
                  <div class="title-wrapper">
                    <input
                        v-if="isInputList[index]"
                        type="text"
                        class="flow-input editable-title-input"
                        @blur="blurEvent(index)"
                        @focus="$event.currentTarget.select()"
                        v-focus
                        v-model="item.nodeName"
                    />
                    <span v-else class="editable-title" @click="clickEvent(index)" :style="`color: rgb(${textColors[nodeConfig.nodeType]});`">{{ item.nodeName }}</span>
                    <span class="priority-title" @click="setPerson(item.priorityLevel)">优先级{{ item.priorityLevel }}</span>
                    <i class="flowicon flowicon-close close" @click="delTerm(index)"></i>
                  </div>
                  <div class="sort-right" v-if="index != nodeConfig.conditionExpressionList.length - 1" @click="arrTransfer(index)">&gt;</div>
                  <div class="content 4444" @click="setPerson(item.priorityLevel)">{{ $func.conditionStr(nodeConfig, index) }}</div>
                  <div class="error_tip" v-if="isTried && item.error">
                    <i class="flowicon flowicon-exclamation-circle"></i>
                  </div>
                </div>
                <addNode v-model:childNodeP="item.childNode" />
              </div>
            </div>
            <nodeWrap v-if="item.childNode" v-model:nodeConfig="item.childNode" />
            <template v-if="index == 0">
              <div class="top-left-cover-line"></div>
              <div class="bottom-left-cover-line"></div>
            </template>
            <template v-if="index === nodeConfig.conditionExpressionList.length - 1">
              <div class="top-right-cover-line"></div>
              <div class="bottom-right-cover-line"></div>
            </template>
          </div>
        </div>
        <addNode v-model:childNodeP="nodeConfig.childNode" />
      </div>
    </div>
    <nodeWrap v-if="nodeConfig.childNode" v-model:nodeConfig="nodeConfig.childNode"/>
  </div>

</template>
<script setup>
import { onMounted, ref, watch, getCurrentInstance, computed } from "vue";
import addNode from "./addNode.vue"
import $func from "../utils/index";
import { useStore } from '../stores/index'
import {bgColors, placeholderList, textColors} from '../utils/const'
import { NodeUtils } from '../utils/nodeUtils'
let _uid = getCurrentInstance().uid;

let props = defineProps({
    nodeConfig: {
        type: Object,
        default: () => ({}),
    },
    flowPermission: {
        type: Object,
        default: () => ([]),
    },
});


let defaultText = computed(() => {
    return placeholderList[props.nodeConfig.nodeType]
});
let showText = computed(() => {
    if (props.nodeConfig.nodeType === 1) return $func.arrToStr(props.flowPermission) || '所有人';
    if (props.nodeConfig.nodeType === 5) return $func.copyerStr(props.nodeConfig);
  if (props.nodeConfig.nodeType === 2) {
    let s= $func.setApproverStr(props.nodeConfig);
    console.log(s,'s')
    return  s
  }
});
props.nodeConfig.nodeDisplay = showText;

let isInputList = ref([]);
let isInput = ref(false);
const resetConditionNodesErr = () => {
    for (var i = 0; i < props.nodeConfig.conditionExpressionList.length; i++) {
        let conditionTitle= $func.conditionStr(props.nodeConfig, i);
        props.nodeConfig.conditionExpressionList[i].error = conditionTitle == "请设置条件" && i != props.nodeConfig.conditionExpressionList.length - 1;
        props.nodeConfig.conditionExpressionList[i].isDefault = 0;
        props.nodeConfig.conditionExpressionList[i].nodeDisplay = conditionTitle=='null' || conditionTitle==''?props.nodeConfig.conditionExpressionList[i].nodeDisplay:conditionTitle;
    }
    let maxLen = props.nodeConfig.conditionExpressionList.length-1;
    let node = props.nodeConfig.conditionExpressionList[maxLen];
    if(node && node.conditionList.length <= 0){
        node.isDefault = 1;
        node.error = false;
    }
}
onMounted(() => {
    if (props.nodeConfig.nodeType === 2) {
      let s =$func.setApproverStr(props.nodeConfig);
        props.nodeConfig.error = s === "请选择"
    } else if (props.nodeConfig.nodeType === 6) {
        props.nodeConfig.error = !$func.copyerStr(props.nodeConfig);
    } else if (props.nodeConfig.nodeType === 4) {
        resetConditionNodesErr()
    }
});
let emits = defineEmits(["update:flowPermission", "update:nodeConfig"]);
let store = useStore();
let {
    setPromoter,
    setApprover,
    setCopyer,
    setCondition,
    setFlowPermission,
    setApproverConfig,
    setCopyerConfig,
    setConditionsConfig,
} = store;
let isTried = computed(()=> store.isTried)
let flowPermission1 = computed(()=> store.flowPermission1)
let approverConfig1 = computed(()=> store.approverConfig1)
let copyerConfig1 = computed(()=> store.copyerConfig1)
let conditionsConfig1 = computed(()=> store.conditionsConfig1)

watch(flowPermission1, (flow) => {
    if (flow.flag && flow.id === _uid) {
        emits("update:flowPermission", flow.value);
    }
});
watch(approverConfig1, (approver) => {
    if (approver.flag && approver.id === _uid) {
        emits("update:nodeConfig", approver.value);
    }
});
watch(copyerConfig1, (copyer) => {
    if (copyer.flag && copyer.id === _uid) {
        emits("update:nodeConfig", copyer.value);
    }
});
watch(conditionsConfig1, (condition) => {
    if (condition.flag && condition.id === _uid) {
        emits("update:nodeConfig", condition.value);
    }
});

const clickEvent = (index) => {
    if (index || index === 0) {
        isInputList.value[index] = true;
    } else {
        isInput.value = true;
    }
};
const blurEvent = (index) => {
    if (index || index === 0) {
        isInputList.value[index] = false;
        props.nodeConfig.conditionExpressionList[index].nodeName = props.nodeConfig.conditionExpressionList[index].nodeName || "条件";
    } else {
        isInput.value = false;
        props.nodeConfig.nodeName = props.nodeConfig.nodeName || defaultText
    }
};
const delNode = () => {
    emits("update:nodeConfig", props.nodeConfig.childNode);
};
const addTerm = () => {
    let len = props.nodeConfig.conditionExpressionList.length + 1;
    let n_name='条件' + len;
    props.nodeConfig.conditionExpressionList.push(NodeUtils.createConditionNode(n_name,null,len,0));
    resetConditionNodesErr()
    emits("update:nodeConfig", props.nodeConfig);
};
const delTerm = (index) => {
    props.nodeConfig.conditionExpressionList.splice(index, 1);
    props.nodeConfig.conditionExpressionList.map((item, index) => {
        item.priorityLevel = index + 1;
        item.nodeName = `条件${index + 1}`;
    });
    resetConditionNodesErr()
    emits("update:nodeConfig", props.nodeConfig);
    if (props.nodeConfig.conditionExpressionList.length == 1) {
        if (props.nodeConfig.childNode) {
            if (props.nodeConfig.conditionExpressionList[0].childNode) {
                reData(props.nodeConfig.conditionExpressionList[0].childNode, props.nodeConfig.childNode);
            } else {
                props.nodeConfig.conditionExpressionList[0].childNode = props.nodeConfig.childNode;
            }
        }
        emits("update:nodeConfig", props.nodeConfig.conditionExpressionList[0].childNode);
    }
};
const reData = (data, addData) => {
    if (!data.childNode) {
        data.childNode = addData;
    } else {
        reData(data.childNode, addData);
    }
};
const setPerson = (priorityLevel) => {
    var { nodeType } = props.nodeConfig;
    if (nodeType === 1) {
        setPromoter(true);
        setFlowPermission({
            value: props.flowPermission,
            flag: false,
            id: _uid,
        });
    } else if (nodeType === 2) {
        setApprover(true);
        setApproverConfig({
            value: {
                ...JSON.parse(JSON.stringify(props.nodeConfig)),
                ...{ approvalUserType: props.nodeConfig.approvalUserType ? props.nodeConfig.approvalUserType : '1' },
            },
            flag: false,
            id: _uid,
        });
    } else if (nodeType === 5) {
        setCopyer(true);
        setCopyerConfig({
            value: JSON.parse(JSON.stringify(props.nodeConfig)),
            flag: false,
            id: _uid,
        });
    } else {
        setCondition(true);
        setConditionsConfig({
            value: JSON.parse(JSON.stringify(props.nodeConfig)),
            priorityLevel,
            flag: false,
            id: _uid,
        });
    }
};
const arrTransfer = (index, type = 1) => {
    //向左-1,向右1
    props.nodeConfig.conditionExpressionList[index] = props.nodeConfig.conditionExpressionList.splice(
        index + type,
        1,
        props.nodeConfig.conditionExpressionList[index]
    )[0];
    props.nodeConfig.conditionExpressionList.map((item, index) => {
        item.priorityLevel = index + 1;
    });
    resetConditionNodesErr()
    emits("update:nodeConfig", props.nodeConfig);
};
</script>
<style lang="less" >
.flow-disigner-node-wrap{

  .auto-judge:hover .editable-title,
  .node-wrap-box:hover .editable-title {
    border-bottom: 1px dashed #fff
  }

  .auto-judge:hover .editable-title.editing,
  .node-wrap-box:hover .editable-title.editing {
    text-decoration: none;
    border: 1px solid #d9d9d9
  }

  .auto-judge:hover .editable-title {
    border-color: #15bc83
  }


  .editable-title {
    line-height: 15px;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    border-bottom: 1px dashed transparent
  }

  .editable-title:before {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    right: 40px
  }

  .editable-title:hover {
    border-bottom: 1px dashed #fff
  }

  .editable-title-input {
    flex: none;
    height: 18px;
    padding-left: 4px;
    text-indent: 0;
    font-size: 12px;
    line-height: 18px;
    z-index: 1
  }

  .editable-title-input:hover {
    text-decoration: none
  }
  .top-left-cover-line {
    left: -1px
  }

  .top-left-cover-line,
  .top-right-cover-line {
    position: absolute;
    height: 8px;
    width: 50%;
    background-color: #f5f5f7;
    top: -4px
  }

  .top-right-cover-line {
    right: -1px
  }

  .bottom-left-cover-line {
    left: -1px
  }

  .bottom-left-cover-line,
  .bottom-right-cover-line {
    position: absolute;
    height: 8px;
    width: 50%;
    background-color: #f5f5f7;
    bottom: -4px
  }

  .bottom-right-cover-line {
    right: -1px
  }


  .node-wrap-box {
    display: -ms-inline-flexbox;
    display: inline-flex;
    -webkit-box-orient: vertical;
    -webkit-box-direction: normal;
    -ms-flex-direction: column;
    flex-direction: column;
    position: relative;
    width: 220px;
    min-height: 72px;
    flex-shrink: 0;
    background: #fff;
    border-radius: 4px;
    cursor: pointer
  }
  .node-wrap-box:after {
    pointer-events: none;
    content: "";
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 2;
    border-radius: 4px;
    border: 1px solid transparent;
    transition: all .1s cubic-bezier(.645, .045, .355, 1);
    box-shadow: 0 2px 5px 0 rgba(0, 0, 0, .1)
  }

  .node-wrap-box.active:after,
  .node-wrap-box:active:after,
  .node-wrap-box:hover:after {
    border: 1px solid #3296fa;
    box-shadow: 0 0 6px 0 rgba(50, 150, 250, .3)
  }

  .node-wrap-box.active .close,
  .node-wrap-box:active .close,
  .node-wrap-box:hover .close {
    display: block
  }

  .node-wrap-box.error:after {
    border: 1px solid #f25643;
    box-shadow: 0 2px 5px 0 rgba(0, 0, 0, .1)
  }

  .node-wrap-box .title {
    position: relative;
    display: flex;
    align-items: center;
    padding-left: 16px;
    padding-right: 30px;
    width: 100%;
    height: 24px;
    line-height: 24px;
    font-size: 12px;
    color: #fff;
    text-align: left;
    background: #576a95;

  }

  .node-wrap-box .title .iconfont {
    font-size: 12px;
    margin-right: 5px
  }

  .node-wrap-box .placeholder {
    color: #bfbfbf
  }

  .node-wrap-box .close {
    display: none;
    position: absolute;
    right: 10px;
    top: 50%;
    transform: translateY(-50%);
    width: 20px;
    height: 20px;
    font-size: 14px;
    color: #fff;
    border-radius: 50%;
    text-align: center;
    line-height: 20px
  }

  .node-wrap-box .content {
    position: relative;
    font-size: 14px;
    padding: 16px;
    padding-right: 30px
  }

  .node-wrap-box .content .text {
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical
  }

  .node-wrap-box .content .arrow {
    position: absolute;
    right: 10px;
    top: 50%;
    transform: translateY(-50%);
    width: 20px;
    height: 14px;
    font-size: 14px;
    color: #979797
  }

  .start-node.node-wrap-box .content .text {
    display: block;
    white-space: nowrap
  }

  .node-wrap-box:before {
    content: "";
    position: absolute;
    top: -12px;
    left: 50%;
    -webkit-transform: translateX(-50%);
    transform: translateX(-50%);
    width: 0;
    height: 4px;
    border-style: solid;
    border-width: 8px 6px 4px;
    border-color: #cacaca transparent transparent;
    background: #f5f5f7
  }

  .node-wrap-box.start-node:before {
    content: none
  }

  .flow-input {
    font-family: Chinese Quote, -apple-system, BlinkMacSystemFont, Segoe UI, PingFang SC, Hiragino Sans GB, Microsoft YaHei, Helvetica Neue, Helvetica, Arial, sans-serif;
    font-variant: tabular-nums;
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    list-style: none;
    position: relative;
    display: inline-block;
    padding: 4px 11px;
    width: 70%;
    height: 20px;
    font-size: 14px;
    line-height: 1.5;
    color: rgba(0, 0, 0, .65);
    background-color: #fff;
    background-image: none;
    border: 1px solid #d9d9d9;
    border-radius: 4px;
    transition: all .3s
  }

  .flow-input::-moz-placeholder {
    color: #bfbfbf;
    opacity: 1
  }

  .flow-input:-ms-input-placeholder {
    color: #bfbfbf
  }

  .flow-input::-webkit-input-placeholder {
    color: #bfbfbf
  }

  .flow-input:focus,
  .flow-input:hover {
    border-color: #40a9ff;
    border-right-width: 1px !important
  }

  .flow-input:focus {
    outline: 0;
    box-shadow: 0 0 0 2px rgba(24, 144, 255, .2)
  }

  .flowicon {
    display: inline-block;
    font-style: normal;
    vertical-align: baseline;
    text-align: center;
    text-transform: none;
    line-height: 1;
    text-rendering: optimizeLegibility;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale
  }

  .flowicon:before {
    display: block;
    font-family: flow !important
  }

  .flowicon-close:before {
    /*"\E633" */
    content: url("~@/assets/images/add-close1.png");
  }

  .flowicon-right:before {
    content: url("~@/assets/images/jiaojiao.png");
  }

  .flowicon-exclamation-circle {
    color: rgb(242, 86, 67)
  }

  .flowicon-exclamation-circle:before {
    content: url("~@/assets/images/warning.png");
  }

  .flowicon-left:before {
    content: url("~@/assets/images/jiaojiao1.png");
  }

  .flowicon-close-circle:before {
    content: url("~@/assets/images/cancel.png");
  }

  .flowicon-delete:before {
    content: url("~@/assets/images/delete.png");
  }
}
.node-wrap-box{


  .banner{
    height: 4px;
    background: red;
    border-radius: 4px 4px 0 0
  }
}
.error_tip {
    position: absolute;
    top: 0px;
    right: 0px;
    transform: translate(150%, 0px);
    font-size: 24px;
}

.promoter_person .el-dialog__body {
    padding: 10px 20px 14px 20px;
}

.selected_list {
    margin-bottom: 20px;
    line-height: 30px;
}

.selected_list span {
    margin-right: 10px;
    padding: 3px 6px 3px 9px;
    line-height: 12px;
    white-space: nowrap;
    border-radius: 2px;
    border: 1px solid rgba(220, 220, 220, 1);
}

.selected_list img {
    margin-left: 5px;
    width: 7px;
    height: 7px;
    cursor: pointer;
}
</style>
